package com.bjc.platform.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bjc.platform.dao.GenericDao;
import com.bjc.platform.model.AdmAuthority;
import com.bjc.platform.model.AdmRole;
import com.bjc.platform.repository.AdmRoleRepo;
import com.bjc.platform.service.AdmAuthorityService;
import com.bjc.platform.service.AdmRoleService;
import com.bjc.platform.vo.ExtTreeModel;

@Service
public class AdmRoleServiceImpl extends BaseServiceImpl implements
		AdmRoleService {

	@Autowired
	AdmRoleRepo admRoleRepo;

	@Autowired
	GenericDao genericDao;

	@Autowired
	AdmAuthorityService admAuthorityService;

	@Override
	public List<Map<String, Object>> getAllWithUserId(Long userId) {
		return genericDao.queryBySqlToMap(roleSql.toString(),
				new Object[] { userId });
	}

	@Override
	public AdmRole get(Long id) {
		return admRoleRepo.findOne(id);
	}

	@Override
	@Transactional
	public AdmRole save(AdmRole role) {
		admAuthorityService.deleteByRoleId(role.getId());
		AdmRole admRole = admRoleRepo.save(role);
		if (role.getAuthorities() != null) {
			for (AdmAuthority auth : role.getAuthorities()) {
				auth.setAdmRole(admRole);
			}
			admAuthorityService.sava(role.getAuthorities());
		}
		return admRole;
	}

	@Override
	public int validRoleCheck(AdmRole role) {
		StringBuilder sb = new StringBuilder();
		if (role != null) {
			sb.append("from AdmRole r where r.flag = 1 and LOWER(r.roleName) = LOWER('");
			sb.append(role.getRoleName()).append("')");
			if (role.getId() != null) {
				sb.append(" and r.id <> ").append(role.getId());
			}

			Map<String, Object> params = null;
			Long count = genericDao.countByJpql(sb.toString(), params);
			if (count > 0) {
				return EXIST_ROLE_NAME;
			}
		}

		return VALID_ROLE;
	}

	@Override
	public void delete(AdmRole role) {
		admRoleRepo.delete(role);
	}

	@Override
	public List<AdmRole> getAll() {
		return admRoleRepo.findByFlag(true);
	}

	@Override
	public List<Map<String, Object>> getRoleForTree() {
		return genericDao.queryBySqlToMap(roleTree.toString(), (Map)null);
	}

	/**
	 * 查询所有角色, 如果当前用户属于某角色, 则字段 checked值 为'checked'
	 */
	private static StringBuilder roleSql = new StringBuilder();
	static {
		roleSql.append(" SELECT ");
		roleSql.append(" r.id,r.role_name roleName, r.discription");
		roleSql.append(" , CASE WHEN ur.role_id IS NOT NULL then 'checked' ELSE NULL END checked ");
		roleSql.append(" FROM ");
		roleSql.append(" adm_role r ");
		roleSql.append(" LEFT JOIN adm_user_role ur ON ur.role_id = r.id ");
		roleSql.append(" AND ur.user_id = ? ");
		roleSql.append(" WHERE r.flag = 1");
	}

	public static StringBuilder roleTree = new StringBuilder();
	static {
		roleTree.append("SELECT ");
		roleTree.append("r.id ").append(ExtTreeModel.ID);
		roleTree.append(",r.role_name ").append(ExtTreeModel.NAME);
		roleTree.append(",r.role_code ");
		roleTree.append(" FROM adm_role r WHERE r.flag = 1 ORDER BY r.sort, r.id");
	}
}
